package com.liaoban.app.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.adapter.RecommendAdapter;
import com.liaoban.app.api.type.Anchor;
import com.liaoban.app.api.type.request.WuserTypeRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.Constant;
import com.liaoban.app.constants.ExtraParam;
import com.liaoban.app.ui.base.BaseListFragment;
import com.liaoban.app.ui.login.LoginActivity;
import com.liaoban.app.ui.anchor.AnchorDetailActivity;

/**
 * 分类推荐
 */
public class ClassRecommendFragment extends BaseListFragment<Anchor> {

    private int mFormId;

    public static Bundle getBundle(int formId) {
        Bundle bundle = new Bundle();
        bundle.putInt(ExtraParam.ID, formId);
        return bundle;
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return new RecommendAdapter(null);
    }

    @Override
    public void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {

    }

    @Override
    public void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        if(!CommonBiz.getInstance().isLogin(getActivity())) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        Anchor anchor = (Anchor) baseQuickAdapter.getData().get(position);
        startActivity(AnchorDetailActivity.newIntent(getActivity(), String.valueOf(anchor.getWuserId())));
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_class_recommend;
    }

    @Override
    public void getData() {
        WuserTypeRequest wuserTypeRequest = new WuserTypeRequest();
        wuserTypeRequest.setFormId(String.valueOf(mFormId));
        wuserTypeRequest.setPage(getPageIndex() + "");
        wuserTypeRequest.setListRows(Constant.PAGE_SIZE + "");
        startTask(CommonBiz.getInstance().wuserType(wuserTypeRequest), listBaseResponse -> {
            doSuc(listBaseResponse.data, listBaseResponse.totalPageCount);
        });
    }

    @Override
    protected void setRecyclerView(RecyclerView recyclerView) {
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    protected void initView(View contentView) {
        initData();
        super.initView(contentView);
    }

    private void initData() {
        Bundle bundle = getArguments();//从activity传过来的Bundle
        if (bundle != null) {
            mFormId = bundle.getInt(ExtraParam.ID);
        }
    }
}
